#include "SystrayIcon.h"
#include "global.h"
#include <iostream>
#include <QApplication>
#include <QVBoxLayout>

SystrayIcon * SystrayIcon::GlobalSystray = 0;

SystrayIcon * SystrayIcon::getSystrayIcon(){
    if (GlobalSystray == 0)
	GlobalSystray = new SystrayIcon();
    return GlobalSystray;
}

SystrayIcon::SystrayIcon(QObject * parent):QSystemTrayIcon(parent){
    DEBUG_F();
    if (!QSystemTrayIcon::isSystemTrayAvailable()) 
	std::cout << ":: system tray is not available right now." << std::endl;
    QApplication::setQuitOnLastWindowClosed(false);

    setObjectName(tr("systray"));
    
    Options * globalOptions = Options::getGlobalOptions();
    setIcon(QIcon(":/icon/systrayIcon.png"));
    setVisible(globalOptions->getOption("isStayOnSystray"));

    dock = new QDockWidget(tr("News..."));
    dock ->setFeatures(QDockWidget::DockWidgetClosable |QDockWidget::DockWidgetMovable);
    dock ->setObjectName("dock");
    dock ->resize(202,114);
    
    QWidget * dockContent = new QWidget(dock);
    QVBoxLayout *layout = new QVBoxLayout(dockContent);
    list = new QListWidget(dockContent);
    list-> setObjectName("list");
    layout->addWidget(list);
    
    button = new QPushButton(tr("Open All"), dock);
    button ->setObjectName("button");
    button ->setFlat(true);
    layout->addWidget(button);

    dock->setWidget(dockContent);

    QMetaObject::connectSlotsByName(dock);
    
    connect(this, SIGNAL(activated ( QSystemTrayIcon::ActivationReason)),
	    this, SLOT(on_activated ( QSystemTrayIcon::ActivationReason)));
    connect(this, SIGNAL(messageClicked()),
	    this, SLOT(on_messageClicked()));
}

void SystrayIcon::alarm(bool value){
    //simply alarm
    if (value)
	showMessage(tr("New message!"), tr("Got a new message !\n\nClicked me to view detail."));
}

void SystrayIcon::on_list_clicked(QListWidgetItem * item){
    DEBUG_F();
    // Show one
    int i = boards.count();
    int j = topics.count();
    int k = articles.count();
    int m = messages.count(); // in case the row number is invalid
    int r = item->data(Qt::UserRole).toInt();
    if (r < i)
	emit boardClicked(boards.takeAt(r));
    else if (r < i + j)
	emit topicClicked(topics.takeAt(r-i));
    else if (r < i + j + k)
	emit articleClicked(articles.takeAt(r -i -j));
    else if (r < i + j + k + m)
	emit messageClicked(messages.takeAt(r - i -j -k));    
}

void SystrayIcon::on_button_clicked(){
    DEBUG_F();
    // Show all
    dock->setVisible(false);
    int i = boards.count();
    while (i -- > 0)
	emit boardClicked(boards.takeAt(0));
    i = topics.count();
    while (i -- > 0)
	emit topicClicked(topics.takeAt(0));
    i = articles.count();
    while (i -- > 0)
	emit articleClicked(articles.takeAt(0));
    i = messages.count();
    while (i -- > 0)
	emit messageClicked(messages.takeAt(0));
}

void SystrayIcon::on_dock_visibilityChanged(bool visible){
}

void SystrayIcon::on_activated(QSystemTrayIcon::ActivationReason reason){
    DEBUG_F();
    if (list->count() == 0)
	emit clicked(reason);
    else if (reason == QSystemTrayIcon::Trigger)
	dock->setVisible(true);
}

void SystrayIcon::on_messageClicked(){
    dock->setVisible(true);
}

void SystrayIcon::addItem(Board * board){
    // verify it ( check the pointer may not work properly)
    if (boards.contains(board)) return;
    int row = boards.count();
    QListWidgetItem * item = new QListWidgetItem(board->name, list);
    item->setData(Qt::UserRole, row);
    list->insertItem(row, item);
    boards << board;
    alarm();
}

void SystrayIcon::addItem(Topic * topic){
    if (topics.contains(topic)) return;
    int row =boards.count() + topics.count();
    QListWidgetItem * item = new QListWidgetItem(topic->name, list);
    item->setData(Qt::UserRole, row);
    list->insertItem(row, item);
    topics << topic;
    alarm();
}

void SystrayIcon::addItem(Article * article){
    if (articles.contains(article)) return;
    int row = boards.count() + topics.count() + articles.count();
    QListWidgetItem * item = new QListWidgetItem(article->title, list);
    item->setData(Qt::UserRole, row);
    list->insertItem(row, item);
    articles << article;
    alarm();
}

void SystrayIcon::addItem(QString message){
    if (messages.contains(message)) return;
    int row = boards.count() + topics.count() + articles.count() + messages.count();
    QListWidgetItem * item = new QListWidgetItem(message, list);
    item->setData(Qt::UserRole, row);
    list->insertItem(row , item);
    messages << message;
    alarm();
}
